<!doctypehtml><html class="sidebar-visible no-js light"lang=en><head><meta charset=UTF-8><title>One-liner introduction - Perl One-Liners Guide</title><meta content="text/html; charset=utf-8"http-equiv=Content-Type><meta content="Example based guide for text processing with Perl from the command line"name=description><meta content=width=device-width,initial-scale=1 name=viewport><meta content=#ffffff name=theme-color><meta content="Perl One-Liners Guide"property=og:title><meta content=website property=og:type><meta content="Example based guide for text processing with Perl from the command line"property=og:description><meta content=https://learnbyexample.github.io/learn_perl_oneliners/ property=og:url><meta content=https://raw.githubusercontent.com/learnbyexample/learn_perl_oneliners/main/images/perl_oneliners_ls.png property=og:image><meta content=1280 property=og:image:width><meta content=720 property=og:image:height><meta content=summary_large_image property=twitter:card><meta content=@learn_byexample property=twitter:site><link href="favicon.svg" rel=icon><link rel="shortcut icon"href="favicon.png"><link href="css/variables.css" rel=stylesheet><link href="css/general.css" rel=stylesheet><link href="css/chrome.css" rel=stylesheet><link href="FontAwesome/css/font-awesome.css" rel=stylesheet><link href="fonts/fonts.css" rel=stylesheet><link href="highlight.css" rel=stylesheet><link href="tomorrow-night.css" rel=stylesheet><link href="ayu-highlight.css" rel=stylesheet><link href="style.css" rel=stylesheet><body><script>var path_to_root = "";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";</script><script>try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }</script><script>var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');</script><script>var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);</script><nav aria-label="Table of contents"class=sidebar id=sidebar><div class=sidebar-scrollbox><ol class=chapter><li class="chapter-item expanded affix"><a href="cover.html">Cover</a><li class="chapter-item expanded affix"><a href="buy.html">Buy PDF/EPUB versions</a><li class="chapter-item expanded"><a href="preface.html"><strong aria-hidden=true>1.</strong> Preface</a><li class="chapter-item expanded"><a class=active href="one-liner-introduction.html"><strong aria-hidden=true>2.</strong> One-liner introduction</a><li class="chapter-item expanded"><a href="line-processing.html"><strong aria-hidden=true>3.</strong> Line processing</a><li class="chapter-item expanded"><a href="in-place-file-editing.html"><strong aria-hidden=true>4.</strong> In-place file editing</a><li class="chapter-item expanded"><a href="field-separators.html"><strong aria-hidden=true>5.</strong> Field separators</a><li class="chapter-item expanded"><a href="record-separators.html"><strong aria-hidden=true>6.</strong> Record separators</a><li class="chapter-item expanded"><a href="using-modules.html"><strong aria-hidden=true>7.</strong> Using modules</a><li class="chapter-item expanded"><a href="multiple-file-input.html"><strong aria-hidden=true>8.</strong> Multiple file input</a><li class="chapter-item expanded"><a href="processing-multiple-records.html"><strong aria-hidden=true>9.</strong> Processing multiple records</a><li class="chapter-item expanded"><a href="two-file-processing.html"><strong aria-hidden=true>10.</strong> Two file processing</a><li class="chapter-item expanded"><a href="dealing-with-duplicates.html"><strong aria-hidden=true>11.</strong> Dealing with duplicates</a><li class="chapter-item expanded"><a href="perl-rename-command.html"><strong aria-hidden=true>12.</strong> Perl rename command</a><li class="chapter-item expanded"><a href="Exercise_solutions.html"><strong aria-hidden=true>13.</strong> Exercise Solutions</a></li><br><hr><li class="chapter-item expanded"><i class="fa fa-github"id=git-repository-button></i><a href=https://github.com/learnbyexample/learn_perl_oneliners>   Source code</a><li class="chapter-item expanded"><i class="fa fa-home"id=home-button></i><a href="../index.html">   My Blog</a><li class="chapter-item expanded"><i class="fa fa-book"id=book-button></i><a href="../books.html">   My Books</a><li class="chapter-item expanded"><i class="fa fa-envelope"id=mail-button></i><a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>   learnbyexample weekly</a><li class="chapter-item expanded"><i class="fa fa-twitter"id=twitter-button></i><a href=https://twitter.com/learn_byexample>   Twitter</a></ol></div><div class=sidebar-resize-handle id=sidebar-resize-handle></div></nav><div class=page-wrapper id=page-wrapper><div class=page><div id=menu-bar-hover-placeholder></div><div class="menu-bar sticky bordered"id=menu-bar><div class=left-buttons><button aria-label="Toggle Table of Contents"title="Toggle Table of Contents"aria-controls=sidebar class=icon-button id=sidebar-toggle type=button><i class="fa fa-bars"></i></button><button aria-label="Change theme"title="Change theme"aria-controls=theme-list aria-expanded=false aria-haspopup=true class=icon-button id=theme-toggle type=button><i class="fa fa-paint-brush"></i></button><ul aria-label=Themes class=theme-popup id=theme-list role=menu><li role=none><button class=theme id=light role=menuitem>Light (default)</button><li role=none><button class=theme id=rust role=menuitem>Rust</button><li role=none><button class=theme id=coal role=menuitem>Coal</button><li role=none><button class=theme id=navy role=menuitem>Navy</button><li role=none><button class=theme id=ayu role=menuitem>Ayu</button></ul><button aria-label="Toggle Searchbar"title="Search. (Shortkey: s)"aria-controls=searchbar aria-expanded=false aria-keyshortcuts=S class=icon-button id=search-toggle type=button><i class="fa fa-search"></i></button></div><h1 class=menu-title>Perl One-Liners Guide</h1><div class=right-buttons><a aria-label=Blog href="../index.html" title=Blog> <i class="fa fa-home"id=home-button></i> </a><a aria-label=Twitter href=https://twitter.com/learn_byexample title=Twitter> <i class="fa fa-twitter"id=twitter-button></i> </a><a aria-label="Git repository"title="Git repository"href=https://github.com/learnbyexample/learn_perl_oneliners> <i class="fa fa-github"id=git-repository-button></i> </a></div></div><div class=hidden id=search-wrapper><form class=searchbar-outer id=searchbar-outer><input placeholder="Search this book ..."aria-controls=searchresults-outer aria-describedby=searchresults-header id=searchbar name=searchbar type=search></form><div class="searchresults-outer hidden"id=searchresults-outer><div class=searchresults-header id=searchresults-header></div><ul id=searchresults></ul></div></div><script>document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });</script><div class=content id=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=one-liner-introduction><a class=header href="one-liner-introduction.html#one-liner-introduction">One-liner introduction</a></h1><p>This chapter will give an overview of Perl syntax for command line usage. You'll see examples to understand what kind of problems are typically suited for one-liners.<h2 id=why-use-perl-for-one-liners><a class=header href="one-liner-introduction.html#why-use-perl-for-one-liners">Why use Perl for one-liners?</a></h2><p>I assume that you are already familiar with use cases where the command line is more productive compared to GUI. See also this series of articles titled <a href=https://blog.sanctum.geek.nz/series/unix-as-ide/>Unix as IDE</a>.<p>A shell utility like Bash provides built-in commands and scripting features to easily solve and automate various tasks. External commands like <code>grep</code>, <code>sed</code>, <code>awk</code>, <code>sort</code>, <code>find</code>, <code>parallel</code>, etc help to solve a wide variety of text processing tasks. These tools are often combined to work together along with shell features like pipelines, wildcards and loops. You can use Perl as an alternative to such external tools and also complement them for some use cases.<p>Here are some sample text processing tasks that you can solve using Perl one-liners. Options and related details will be explained later.<pre><code class=language-bash># change ; to #
# but don't change ; within single or double quotes
perl -pe 's/(?:\x27;\x27|";")(*SKIP)(*F)|;/#/g'

# retain only the first copy of duplicated lines
# uses the built-in module List::Util
perl -MList::Util=uniq -e 'print uniq <>'

# extract only IPv4 addresses
# uses a third-party module Regexp::Common
perl -MRegexp::Common=net -nE 'say $& while /$RE{net}{IPv4}/g'
</code></pre><p>Here are some stackoverflow questions that I've answered with simpler Perl solution compared to other CLI tools:<ul><li><a href=https://stackoverflow.com/q/42554684/4082052>replace string with incrementing value</a><li><a href=https://stackoverflow.com/q/48920626/4082052>sort rows in csv file without header & first column</a><li><a href=https://stackoverflow.com/q/63681983/4082052>reverse matched pattern</a><li><a href=https://stackoverflow.com/q/49765879/4082052>append zeros to list</a><li><a href=https://stackoverflow.com/q/62241101/4082052>arithmetic replacement in a text file</a><li><a href=https://stackoverflow.com/q/45571828/4082052>reverse complement DNA sequence for a specific field</a></ul><p>The selling point of Perl over tools like <code>grep</code>, <code>sed</code> and <code>awk</code> includes feature rich regular expression engine and standard/third-party modules. Another advantage is that Perl is more portable, given the many differences between GNU, BSD and other such implementations. The main disadvantage is that Perl is likely to be verbose and slower for features that are supported out of the box by those tools.<blockquote><p><img alt=info src="images/info.svg"> See also <a href=https://unix.stackexchange.com/q/303044/109046>unix.stackexchange: when to use grep, sed, awk, perl, etc</a>.</blockquote><h2 id=installation-and-documentation><a class=header href="one-liner-introduction.html#installation-and-documentation">Installation and Documentation</a></h2><p>If you are on a Unix-like system, you are most likely to already have some version of Perl installed. See <a href=https://www.cpan.org/src/README.html>cpan: Perl Source</a> for instructions to install the latest Perl version from source. <code>perl v5.38.0</code> is used for all the examples shown in this book.<p>You can use the <code>perldoc</code> command to access documentation from the command line. You can visit <a href=https://perldoc.perl.org/>https://perldoc.perl.org/</a> if you wish to read it online, which also has a handy search feature. Here are some useful links to get started:<ul><li><a href=https://perldoc.perl.org/perl#Overview>perldoc: overview</a><li><a href=https://perldoc.perl.org/perlintro>perldoc: perlintro</a><li><a href=https://perldoc.perl.org/perlfaq>perldoc: faqs</a></ul><h2 id=command-line-options><a class=header href="one-liner-introduction.html#command-line-options">Command line options</a></h2><p><code>perl -h</code> gives the list of all command line options, along with a brief description. See <a href=https://perldoc.perl.org/perlrun>perldoc: perlrun</a> for documentation on these command switches.<div class=table-wrapper><table><thead><tr><th><strong>Option</strong><th><strong>Description</strong><tbody><tr><td><code>-0[octal]</code><td>specify record separator (<code>\0</code>, if no argument)<tr><td><code>-a</code><td>autosplit mode with <code>-n</code> or <code>-p</code> (splits <code>$_</code> into <code>@F</code>)<tr><td><code>-C[number/list]</code><td>enables the listed Unicode features<tr><td><code>-c</code><td>check syntax only (runs <code>BEGIN</code> and <code>CHECK</code> blocks)<tr><td><code>-d[t][:MOD]</code><td>run program under debugger or module <code>Devel::MOD</code><tr><td><code>-D[number/letters]</code><td>set debugging flags (argument is a bit mask or alphabets)<tr><td><code>-e commandline</code><td>one line of program (several <code>-e</code>'s allowed, omit programfile)<tr><td><code>-E commandline</code><td>like <code>-e</code>, but enables all optional features<tr><td><code>-f</code><td>don't do <code>$sitelib/sitecustomize.pl</code> at startup<tr><td><code>-F/pattern/</code><td><code>split()</code> pattern for <code>-a</code> switch (<code>//</code>'s are optional)<tr><td><code>-g</code><td>read all input in one go (slurp), rather than line-by-line<tr><td><td>(alias for <code>-0777</code>)<tr><td><code>-i[extension]</code><td>edit <code><></code> files in place (makes backup if extension supplied)<tr><td><code>-Idirectory</code><td>specify <code>@INC/#include</code> directory (several <code>-I</code>'s allowed)<tr><td><code>-l[octnum]</code><td>enable line ending processing, specifies line terminator<tr><td><code>-[mM][-]module</code><td>execute <code>use/no module...</code> before executing program<tr><td><code>-n</code><td>assume <code>while (<>) { ... }</code> loop around program<tr><td><code>-p</code><td>assume loop like <code>-n</code> but <code>print</code> line also, like <code>sed</code><tr><td><code>-s</code><td>enable rudimentary parsing for switches after programfile<tr><td><code>-S</code><td>look for programfile using <code>PATH</code> environment variable<tr><td><code>-t</code><td>enable tainting warnings<tr><td><code>-T</code><td>enable tainting checks<tr><td><code>-u</code><td>dump core after parsing program<tr><td><code>-U</code><td>allow unsafe operations<tr><td><code>-v</code><td>print version, patchlevel and license<tr><td><code>-V[:variable]</code><td>print configuration summary (or a single <code>Config.pm</code> variable)<tr><td><code>-w</code><td>enable many useful warnings<tr><td><code>-W</code><td>enable all warnings<tr><td><code>-x[directory]</code><td>ignore text before <code>#!perl</code> line (optionally <code>cd</code> to directory)<tr><td><code>-X</code><td>disable all warnings</table></div><p>This chapter will show examples with the <code>-e</code>, <code>-E</code>, <code>-l</code>, <code>-n</code>, <code>-p</code> and <code>-a</code> options. Some more options will be covered in later chapters, but not all of them are discussed in this book.<h2 id=executing-perl-code><a class=header href="one-liner-introduction.html#executing-perl-code">Executing Perl code</a></h2><p>If you want to execute a Perl program file, one way is to pass the filename as an argument to the <code>perl</code> command.<pre><code class=language-bash>$ echo 'print "Hello Perl\n"' > hello.pl
$ perl hello.pl
Hello Perl
</code></pre><p>For short programs, you can also directly pass the code as an argument to the <code>-e</code> and <code>-E</code> options. See <a href=https://perldoc.perl.org/feature>perldoc: feature</a> for details about the features enabled by the <code>-E</code> option.<pre><code class=language-bash>$ perl -e 'print "Hello Perl\n"'
Hello Perl

# multiple statements can be issued separated by ;
# -l option will be covered in detail later, appends \n to 'print' here
$ perl -le '$x=25; $y=12; print $x**$y'
59604644775390625
# or, use -E and 'say' instead of -l and 'print'
$ perl -E '$x=25; $y=12; say $x**$y'
59604644775390625
</code></pre><h2 id=filtering><a class=header href="one-liner-introduction.html#filtering">Filtering</a></h2><p>Perl one-liners can be used for filtering lines matched by a regular expression (regexp), similar to the <code>grep</code>, <code>sed</code> and <code>awk</code> commands. And similar to many command line utilities, Perl can accept input from both stdin and file arguments.<pre><code class=language-bash># sample stdin data
$ printf 'gate\napple\nwhat\nkite\n'
gate
apple
what
kite

# print lines containing 'at'
# same as: grep 'at' and sed -n '/at/p' and awk '/at/'
$ printf 'gate\napple\nwhat\nkite\n' | perl -ne 'print if /at/'
gate
what

# print lines NOT containing 'e'
# same as: grep -v 'e' and sed -n '/e/!p' and awk '!/e/'
$ printf 'gate\napple\nwhat\nkite\n' | perl -ne 'print if !/e/'
what
</code></pre><p>By default, <code>grep</code>, <code>sed</code> and <code>awk</code> automatically loop over the input content line by line (with newline character as the default line separator). To do so with Perl, you can use the <code>-n</code> and <code>-p</code> options. The <a href="using-modules.html#convert-one-liners-to-pretty-formatted-scripts">O module</a> section shows the code Perl runs with these options.<p>As seen before, the <code>-e</code> option accepts code as a command line argument. Many shortcuts are available to reduce the amount of typing needed. In the above examples, a regular expression (defined by the pattern between a pair of forward slashes) has been used to filter the input. When the input string isn't specified, the test is performed against the special variable <code>$_</code> which has the contents of the current input line (the correct term would be input <strong>record</strong>, as discussed in the <a href="record-separators.html#record-separators">Record separators</a> chapter). <code>$_</code> is also the default argument for many functions like <code>print</code> and <code>say</code>. To summarize:<ul><li><code>/REGEXP/FLAGS</code> is a shortcut for <code>$_ =~ m/REGEXP/FLAGS</code><li><code>!/REGEXP/FLAGS</code> is a shortcut for <code>$_ !~ m/REGEXP/FLAGS</code></ul><blockquote><p><img alt=info src="images/info.svg"> See <a href=https://perldoc.perl.org/perlop#m/PATTERN/msixpodualngc>perldoc: match</a> for help on the <code>m</code> operator.</blockquote><p>Here's an example with file input instead of stdin.<pre><code class=language-bash>$ cat table.txt
brown bread mat hair 42
blue cake mug shirt -7
yellow banana window shoes 3.14

# digits at the end of lines that are not preceded by -
$ perl -nE 'say $& if /(?&LT!-)\d+$/' table.txt
42
14
# if the condition isn't required, capture groups can be used
$ perl -nE 'say /(\d+)$/' table.txt
42
7
14
</code></pre><blockquote><p><img alt=info src="images/info.svg"> The <a href=https://github.com/learnbyexample/learn_perl_oneliners/tree/main/example_files>example_files</a> directory has all the files used in the examples (like <code>table.txt</code> in the above illustration).</blockquote><h2 id=substitution><a class=header href="one-liner-introduction.html#substitution">Substitution</a></h2><p>Use the <code>s</code> operator for search and replace requirements. By default, this operates on <code>$_</code> when the input string isn't provided. For these examples, the <code>-p</code> option is used instead of <code>-n</code>, so that the value of <code>$_</code> is automatically printed after processing each input line. See <a href=https://perldoc.perl.org/perlop#s/PATTERN/REPLACEMENT/msixpodualngcer>perldoc: search and replace</a> for documentation and examples.<pre><code class=language-bash># for each input line, change only the first ':' to '-'
# same as: sed 's/:/-/' and awk '{sub(/:/, "-")} 1'
$ printf '1:2:3:4\na:b:c:d\n' | perl -pe 's/:/-/'
1-2:3:4
a-b:c:d

# for each input line, change all ':' to '-'
# same as: sed 's/:/-/g' and awk '{gsub(/:/, "-")} 1'
$ printf '1:2:3:4\na:b:c:d\n' | perl -pe 's/:/-/g'
1-2-3-4
a-b-c-d
</code></pre><blockquote><p><img alt=info src="images/info.svg"> The <code>s</code> operator modifies the input string it is acting upon if the pattern matches. In addition, it will return the number of substitutions made if successful, otherwise returns a <em>falsy</em> value (empty string or <code>0</code>). You can use the <code>r</code> flag to return the string after substitution instead of in-place modification. As mentioned before, this book assumes you are already familiar with Perl regular expressions. If not, see <a href=https://perldoc.perl.org/perlretut>perldoc: perlretut</a> to get started.</blockquote><h2 id=special-variables><a class=header href="one-liner-introduction.html#special-variables">Special variables</a></h2><p>Brief descriptions for some of the special variables are given below:<ul><li><code>$_</code> contains the input record content<li><code>@F</code> array containing fields (with the <code>-a</code> and <code>-F</code> options) <ul><li><code>$F[0]</code> first field<li><code>$F[1]</code> second field and so on<li><code>$F[-1]</code> last field<li><code>$F[-2]</code> second last field and so on<li><code>$#F</code> index of the last field</ul><li><code>$.</code> number of records (i.e. line number)<li><code>$1</code> backreference to the first capture group<li><code>$2</code> backreference to the second capture group and so on<li><code>$&</code> backreference to the entire matched portion<li><code>%ENV</code> hash containing environment variables</ul><p>See <a href=https://perldoc.perl.org/perlvar#SPECIAL-VARIABLES>perldoc: special variables</a> for documentation.<h2 id=field-processing><a class=header href="one-liner-introduction.html#field-processing">Field processing</a></h2><p>Consider the sample input file shown below with fields separated by a single space character.<pre><code class=language-bash>$ cat table.txt
brown bread mat hair 42
blue cake mug shirt -7
yellow banana window shoes 3.14
</code></pre><p>Here are some examples that are based on specific fields rather than the entire line. The <code>-a</code> option will cause the input line to be split based on whitespaces and the field contents can be accessed using the <code>@F</code> special array variable. Leading and trailing whitespaces will be suppressed, so there's no possibility of empty fields. More details are discussed in the <a href="field-separators.html#default-field-separation">Default field separation</a> section.<pre><code class=language-bash># print the second field of each input line
# same as: awk '{print $2}' table.txt
$ perl -lane 'print $F[1]' table.txt
bread
cake
banana

# print lines only if the last field is a negative number
# same as: awk '$NF&LT0' table.txt
$ perl -lane 'print if $F[-1] < 0' table.txt
blue cake mug shirt -7

# change 'b' to 'B' only for the first field
# same as: awk '{gsub(/b/, "B", $1)} 1' table.txt
$ perl -lane '$F[0] =~ s/b/B/g; print "@F"' table.txt
Brown bread mat hair 42
Blue cake mug shirt -7
yellow banana window shoes 3.14
</code></pre><p>See the <a href="field-separators.html#output-field-separator">Output field separator</a> section for details on using array variables inside double quotes.<h2 id=begin-and-end><a class=header href="one-liner-introduction.html#begin-and-end">BEGIN and END</a></h2><p>You can use a <code>BEGIN{}</code> block when you need to execute something before the input is read and an <code>END{}</code> block to execute something after all of the input has been processed.<pre><code class=language-bash># same as: awk 'BEGIN{print "---"} 1; END{print "%%%"}'
$ seq 4 | perl -pE 'BEGIN{say "---"} END{say "%%%"}'
---
1
2
3
4
%%%
</code></pre><h2 id=env-hash><a class=header href="one-liner-introduction.html#env-hash">ENV hash</a></h2><p>When it comes to automation and scripting, you'd often need to construct commands that can accept input from users, use data from files and the output of a shell command and so on. As mentioned before, this book assumes <code>bash</code> as the shell being used. To access environment variables of the shell, you can use the special hash variable <code>%ENV</code> with the name of the environment variable as a string key.<blockquote><p><img alt=info src="images/info.svg"> Quotes won't be used around <code>hash</code> keys in this book. See <a href=https://stackoverflow.com/q/401556/4082052>stackoverflow: are quotes around hash keys a good practice in Perl?</a> on possible issues if you don't quote the <code>hash</code> keys.</blockquote><pre><code class=language-bash># existing environment variables
# output shown here is for my machine, would differ for you
$ perl -E 'say $ENV{HOME}'
/home/learnbyexample
$ perl -E 'say $ENV{SHELL}'
/bin/bash

# defined along with the command
# note that the variable definition is placed before the command
$ word='hello' perl -E 'say $ENV{word}'
hello
# the characters are preserved as is
$ ip='hi\nbye' perl -E 'say $ENV{ip}'
hi\nbye
</code></pre><p>Here's another example when a regexp is passed as an environment variable content.<pre><code class=language-bash>$ cat anchors.txt
sub par
spar
apparent effort
two spare computers
cart part tart mart

# assume 'r' is a shell variable containing user provided regexp
$ r='\Bpar\B'
$ rgx="$r" perl -ne 'print if /$ENV{rgx}/' anchors.txt
apparent effort
two spare computers
</code></pre><p>You can also make use of the <code>-s</code> option to assign a Perl variable.<pre><code class=language-bash>$ r='\Bpar\B'
$ perl -sne 'print if /$rgx/' -- -rgx="$r" anchors.txt
apparent effort
two spare computers
</code></pre><blockquote><p><img alt=info src="images/info.svg"> As an example, see my repo <a href=https://github.com/learnbyexample/command_help/blob/master/ch>ch: command help</a> for a practical shell script, where commands are constructed dynamically.</blockquote><h2 id=executing-external-commands><a class=header href="one-liner-introduction.html#executing-external-commands">Executing external commands</a></h2><p>You can execute external commands using the <code>system</code> function. See <a href=https://perldoc.perl.org/functions/system>perldoc: system</a> for documentation and details like how string/list arguments are processed before execution.<pre><code class=language-bash>$ perl -e 'system("echo Hello World")'
Hello World

$ perl -e 'system("wc -w &LTanchors.txt")'
12

$ perl -e 'system("seq -s, 10 > out.txt")'
$ cat out.txt
1,2,3,4,5,6,7,8,9,10
</code></pre><p>The return value of <code>system</code> or the special variable <code>$?</code> can be used to act upon the exit status of the command being executed. As per documentation:<blockquote><p><img alt=info src="images/info.svg"> The return value is the exit status of the program as returned by the <code>wait</code> call. To get the actual exit value, shift right by eight</blockquote><pre><code class=language-bash>$ perl -E '$es=system("ls anchors.txt"); say $es'
anchors.txt
0
$ perl -E 'system("ls anchors.txt"); say $?'
anchors.txt
0

$ perl -E 'system("ls xyz.txt"); say $?'
ls: cannot access 'xyz.txt': No such file or directory
512
</code></pre><p>To save the result of an external command, use backticks or the <code>qx</code> operator. See <a href=https://perldoc.perl.org/perlop#qx/STRING/>perldoc: qx</a> for documentation and details like separating out <code>STDOUT</code> and <code>STDERR</code>.<pre><code class=language-bash>$ perl -e '$words = `wc -w &LTanchors.txt`; print $words'
12

$ perl -e '$nums = qx/seq 3/; print $nums'
1
2
3
</code></pre><blockquote><p><img alt=info src="images/info.svg"> See also <a href=https://stackoverflow.com/q/799968/4082052>stackoverflow: difference between backticks, system, and exec</a>.</blockquote><h2 id=summary><a class=header href="one-liner-introduction.html#summary">Summary</a></h2><p>This chapter introduced some of the common options for Perl CLI usage, along with some of the typical text processing examples. While specific purpose CLI tools like <code>grep</code>, <code>sed</code> and <code>awk</code> are usually faster, Perl has a much more extensive standard library and ecosystem. And you do not have to learn a lot if you are already comfortable with Perl but not familiar with those CLI tools. The next section has a few exercises for you to practice the CLI options and text processing use cases.<h2 id=exercises><a class=header href="one-liner-introduction.html#exercises">Exercises</a></h2><blockquote><p><img alt=info src="images/info.svg"> All the exercises are also collated together in one place at <a href=https://github.com/learnbyexample/learn_perl_oneliners/blob/main/exercises/Exercises.md>Exercises.md</a>. For solutions, see <a href=https://github.com/learnbyexample/learn_perl_oneliners/blob/main/exercises/Exercise_solutions.md>Exercise_solutions.md</a>.</blockquote><blockquote><p><img alt=info src="images/info.svg"> The <a href=https://github.com/learnbyexample/learn_perl_oneliners/tree/main/exercises>exercises</a> directory has all the files used in this section.</blockquote><p><strong>1)</strong> For the input file <code>ip.txt</code>, display all lines containing <code>is</code>.<pre><code class=language-bash>$ cat ip.txt
Hello World
How are you
This game is good
Today is sunny
12345
You are funny

##### add your solution here
This game is good
Today is sunny
</code></pre><p><strong>2)</strong> For the input file <code>ip.txt</code>, display the first field of lines <em>not</em> containing <code>y</code>. Consider space as the field separator for this file.<pre><code class=language-bash>##### add your solution here
Hello
This
12345
</code></pre><p><strong>3)</strong> For the input file <code>ip.txt</code>, display all lines containing no more than 2 fields.<pre><code class=language-bash>##### add your solution here
Hello World
12345
</code></pre><p><strong>4)</strong> For the input file <code>ip.txt</code>, display all lines containing <code>is</code> in the second field.<pre><code class=language-bash>##### add your solution here
Today is sunny
</code></pre><p><strong>5)</strong> For each line of the input file <code>ip.txt</code>, replace the first occurrence of <code>o</code> with <code>0</code>.<pre><code class=language-bash>##### add your solution here
Hell0 World
H0w are you
This game is g0od
T0day is sunny
12345
Y0u are funny
</code></pre><p><strong>6)</strong> For the input file <code>table.txt</code>, calculate and display the product of numbers in the last field of each line. Consider space as the field separator for this file.<pre><code class=language-bash>$ cat table.txt
brown bread mat hair 42
blue cake mug shirt -7
yellow banana window shoes 3.14

##### add your solution here
-923.16
</code></pre><p><strong>7)</strong> Append <code>.</code> to all the input lines for the given stdin data.<pre><code class=language-bash>$ printf 'last\nappend\nstop\ntail\n' | ##### add your solution here
last.
append.
stop.
tail.
</code></pre><p><strong>8)</strong> Use the contents of the <code>s</code> variable to display all matching lines from the input file <code>ip.txt</code>. Assume that <code>s</code> doesn't have any regexp metacharacters. Construct the solution such that there's at least one word character immediately preceding the contents of the <code>s</code> variable.<pre><code class=language-bash>$ s='is'

##### add your solution here
This game is good
</code></pre><p><strong>9)</strong> Use <code>system</code> to display the contents of the filename present in the second field of the given input line. Consider space as the field separator.<pre><code class=language-bash>$ s='report.log ip.txt sorted.txt'
$ echo "$s" | ##### add your solution here
Hello World
How are you
This game is good
Today is sunny
12345
You are funny

$ s='power.txt table.txt'
$ echo "$s" | ##### add your solution here
brown bread mat hair 42
blue cake mug shirt -7
yellow banana window shoes 3.14
</code></pre></main><nav aria-label="Page navigation"class=nav-wrapper><a aria-label="Previous chapter"class="mobile-nav-chapters previous"title="Previous chapter"aria-keyshortcuts=Left href="preface.html" rel=prev> <i class="fa fa-angle-left"></i> </a><a aria-label="Next chapter"class="mobile-nav-chapters next"title="Next chapter"aria-keyshortcuts=Right href="line-processing.html" rel=next> <i class="fa fa-angle-right"></i> </a><div style="clear: both"></div></nav></div></div><nav aria-label="Page navigation"class=nav-wide-wrapper><a aria-label="Previous chapter"class="nav-chapters previous"title="Previous chapter"aria-keyshortcuts=Left href="preface.html" rel=prev> <i class="fa fa-angle-left"></i> </a><a aria-label="Next chapter"class="nav-chapters next"title="Next chapter"aria-keyshortcuts=Right href="line-processing.html" rel=next> <i class="fa fa-angle-right"></i> </a></nav></div><script>window.playground_copyable = true;</script><script charset=utf-8 src="elasticlunr.min.js"></script><script charset=utf-8 src="mark.min.js"></script><script charset=utf-8 src="searcher.js"></script><script charset=utf-8 src="clipboard.min.js"></script><script charset=utf-8 src="highlight.js"></script><script charset=utf-8 src="book.js"></script><script src="sidebar.js"></script>